package com.zhiyou.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhiyou.model.Vacate;
import com.zhiyou.service.TeacherManageService;
import com.zhiyou.service.VacateService;
import com.zhiyou.utils.JsonResult;

@RestController
@RequestMapping("vacate/")
public class VacateController {

	@Autowired
	VacateService vacateService;
	@Autowired
	TeacherManageService teacherManageService;
	/**
	 * 根据学生id和审批状态来查询
	 * @param id
	 * @param statue_id
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("list/{id}/{statu_id}")
	public JsonResult show(@PathVariable("id") Integer id, @PathVariable("statu_id") Integer statue_id,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			List<Vacate> list = vacateService.selectBySt_id(id, statue_id);
			return new JsonResult(list, "根据学生id和审批状态来查询成功");
		} catch (Exception e) {
			return new JsonResult(500, "查询失败");
		}
	}

	/**
	 * 根据请假id来查询请假详细信息
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("list/{id}")
	public JsonResult selectById(@PathVariable("id") Integer id, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Vacate vacate = vacateService.selectById(id);
			return new JsonResult(vacate, "根据请假id来查询成功");
		} catch (Exception e) {
			return new JsonResult(500, "查询失败");
		}
	}
	/**
	 * 根据请假id删除请假信息
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@DeleteMapping("delete/{id}")
	public JsonResult delete(@PathVariable("id") Integer id, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			vacateService.delete(id);;
			return new JsonResult("根据请假id来删除成功");
		} catch (Exception e) {
			return new JsonResult(500, "删除失败");
		}
	}
	/**
	 * 根据请假id批量删除请假信息
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("delete")
	public JsonResult deleteAll(int[] ids, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			vacateService.deleteAll(ids);;;
			return new JsonResult("根据请假id批量删除成功");
		} catch (Exception e) {
			return new JsonResult(500, "删除失败");
		}
	}
	/**
	 * 添加一条请假记录
	 * @param vacate
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(value = "add")
	public JsonResult add(Vacate vacate, HttpServletRequest request, HttpServletResponse response) {
		try {
			vacateService.add(vacate);
			return new JsonResult("插入成功");
		} catch (Exception e) {
			return new JsonResult(500, "查询插入失败");
		}
	}

	/**
	 * 查看所有请假信息
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("list")
	public JsonResult selectAll(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<Vacate> list = vacateService.selectAll();
			return new JsonResult(list, "查看所有请假信息成功");
		} catch (Exception e) {
			return new JsonResult(500, "查询失败");
		}
	}

	/**
	 * 根据班级查看请假信息(事假|病假,已审批,未审批)
	 * @param class_id
	 * @param vacate_type
	 * @param statue_id
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("list/{class_id}/{vacate_type}/{statue_id}")
	public JsonResult selectAllClass_id(@PathVariable("class_id") Integer class_id,
			@PathVariable("vacate_type") Integer vacate_type, @PathVariable("statue_id") Integer statue_id,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			
			List<Vacate> list = vacateService.selectAllClass_id(class_id, vacate_type, statue_id);
			return new JsonResult(list, "根据班级查看请假信息(事假|病假,已审批,未审批)成功");
		} catch (Exception e) {
			return new JsonResult(500, "查询失败");
		}
	}
	/**
	 * 批改作业
	 * @param vacate
	 * @param request
	 * @param response
	 * @return
	 */
	@PutMapping("update")
	public JsonResult update(Vacate vacate, HttpServletRequest request, HttpServletResponse response) {
		try {
			System.out.println(vacate);
			vacateService.update(vacate);
			System.out.println(vacate);
			//获取vacate的实现
			Vacate vacate2 = vacateService.selectById(vacate.getVacate_id());
			if(vacate.getVacate_status()==1){				
				teacherManageService.addone(vacate2.getStudent_id(), vacate2.getVacate_commit_time());
			}
			return new JsonResult("修改成功");
		} catch (Exception e) {
			return new JsonResult(500, "修改失败");
		}
	}
}
